package com.order.controller.seller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.order.pojo.ProductCategory;
import com.order.service.ProductCategoryService;
import com.order.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品分类接口")
@RestController
@RequestMapping("/seller/productCategory")
@Slf4j
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @ApiOperation(value = "获取所有商品分类")
    @GetMapping("/")
    public Result getAllProductCategory() {
        List<ProductCategory> categories = productCategoryService.list(new QueryWrapper<ProductCategory>().eq("category_del", 0));
        if (categories == null || categories.isEmpty()) {
            return Result.error("没有商品分类信息");
        }
        return Result.success(categories);
    }

    @ApiOperation(value = "根据ID获取商品分类")
    @GetMapping("{categoryId}")
    public Result getProductCategoryById(@PathVariable("categoryId") Integer categoryId) {
        ProductCategory category = productCategoryService.getById(categoryId);
        if (category == null) {
            return Result.error("商品分类不存在");
        }
        return Result.success(category);
    }

    @ApiOperation(value = "添加商品分类")
    @PostMapping("/add")
    public Result addProductCategory(@RequestBody ProductCategory productCategory) {
        if(productCategoryService.save(productCategory)) {
            return Result.success(null,"添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @ApiOperation(value = "更新商品分类")
    @PutMapping("/update")
    public Result updateProductCategory(@RequestBody ProductCategory productCategory) {
        productCategory.setUpdateTime(DateUtil.now());
        if(productCategoryService.updateById(productCategory)) {
            return Result.success(null,"更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @ApiOperation(value = "删除商品分类")
    @DeleteMapping("/delete/{categoryId}")
    public Result deleteProductCategory(@PathVariable("categoryId") Integer categoryId) {
        ProductCategory category = new ProductCategory(categoryId,1);
        if(productCategoryService.updateById(category)) {
            return Result.success(null,"删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @ApiOperation(value = "根据页面获取所有商品分类")
    @GetMapping("/page")
    public Result getProductCategoryByPage(@RequestParam(value = "pageNum", defaultValue = "1") Integer page,
                                           @RequestParam(value = "pageSize", defaultValue = "8") Integer size,
                                           ProductCategory productCategory) {
        PageInfo<ProductCategory> pageInfo = productCategoryService.getCategoriesByPage(page, size, productCategory);
        return Result.success(pageInfo);
    }
}
